// component.ts
import { Component, OnInit, ViewChild } from '@angular/core';
import { Router, ActivatedRoute } from "@angular/router"; //路由引入
import { JsonApiService } from "app/core/api/json-api.service";
import { config } from '../../shared/smartadmin.config';

declare var $: any;
declare var Viewer: any;
@Component({
    selector: 'sa-damaged-device-return',
    templateUrl: './damaged-device-return.component.html',
    styleUrls: ['./damaged-device-return.css']
})
export class DamagedDeviceReturnComponent implements OnInit {
	tableData: any = []; // table的表单数据
    totalCounts: number = 0; //分页总条数
    curPage: number = 1; //分页当前页
    pageSize: number = 10; //内置的分页每页条数
    activeSelected: string = ''; //row高亮
    // 查询参数
    selectedCompanyId:any = ""; // 公司的id
    selectedCompanyName:any = ""; // 公司的名称
    // selectedDeviceId: any = ""; // 选中的设备ID
    // selectedCarNumber: any = ""; // 选中的车牌号
    startTime: any = ""; // 开始时间
    endTime: any = ""; // 结束时间
    // userName: any = ""; // 操作人
    searchKey: any = ""; // 用户的搜索关键字

    constructor(private router: Router, private activatedRoute: ActivatedRoute, private jsonApiService: JsonApiService) {
        activatedRoute.queryParams.subscribe(queryParams => {
        });
    }
    ngOnInit() {
        this.initDate();
        this.load();
    }

   
    // 时间选择回调
    dateSelect(time) {
        this.startTime = time.startTime;
        this.endTime = time.endTime;
        if(!time.startTime || !time.endTime){
            this.dateNumber = '';
        }
    }
    // 初始化1天的时间
	initDate(){
		let nowTime = new Date();
		this.endTime = common.getTimeMmss(nowTime);
		nowTime.setDate(nowTime.getDate() - 30);
		this.startTime = common.getTimeMmss(nowTime);
		this.specifyTime = {
			startTime: this.startTime,
			endTime: this.endTime,
		}
	}
	dateNumber:any = '30'; //查看天数
    specifyTime: any;
    // 快捷选择天数
	selectDate() {
	    if(!this.dateNumber){
	        this.specifyTime = {
                startTime: '',
                endTime: '',
            }
	        return
	    }
		let nowTime = new Date();
		this.endTime = common.getTimeMmss(nowTime);
		nowTime.setDate(nowTime.getDate() - this.dateNumber);
		this.startTime = common.getTimeMmss(nowTime);
		this.specifyTime = {
			startTime: this.startTime,
			endTime: this.endTime,
		}
	}

    //公司选中回调
    companySelected(data){
        this.selectedCompanyId = data.companyId;
        this.selectedCompanyName = data.companyName;
    }

    // 获取列表数据
    load() {
        let param = {
            "pageSize": this.pageSize,
            "nowPage": this.curPage,
            "parameters": {
                companyId:this.selectedCompanyId,
                // deviceId: this.selectedDeviceId,
                startTime: this.startTime,
                endTime: this.endTime,
                // userName: this.userName,
                searchKey: common.stringTrim(this.searchKey),
            }
        }
        let reqdata = 'gridPager=' + JSON.stringify(param);
        this.jsonApiService.http("deviceDamage/getDeviceDamageForPage.html", reqdata).subscribe(result => {
            let data = []
            if (result.isSuccess && result.exhibitDatas) {
                data = result.exhibitDatas
               
                this.tableData = result.exhibitDatas
                this.totalCounts = result.recordCount;
            }
        });
    }
    // 删除
    rowDelete(row){
        this.jsonApiService.confirm_message("此操作将永久删除数据,确认要删除?").then(()=>{
            this.jsonApiService.http("deviceDamage/deleteById.html",'id='+row.id).subscribe(data=>{
                this.jsonApiService.message(data.message);
                if(data.success){
                    this.load();
                }
            })
        })
    }
    // 分页回调
    paginate(e) {
        this.activeSelected = '';
        this.curPage = e.page + 1;
        this.pageSize = e.rows;
        this.load();
    }
    // 刷新表
    refresh() {
        this.load();
    }
    // 表单查询
    tableSearch() {
        this.curPage = 1;
        this.load();
    }
    /**
     * 图片查看的方法
     */
    imgList: any = [];
    imgLocalList:any=[];
    isViewer2:boolean = false; //默认隐藏
    //显示图片组件
    viewer2: any='';
 	showImg2(row,modal){

		this.imgList = [];
		this.imgLocalList = [];
		this.imgList = JSON.parse(row.picUrl);

		let localImg;
		for(var i=0;i< this.imgList.length;i++){
			// if(this.imgList[i][0] == '/'){
			// 	localImg = config.BaseURL + this.imgList[i].substr(1);
			// }else{
			// 	localImg = config.BaseURL + this.imgList[i][i];
			// }
			if(this.imgList[i].indexOf('http') == -1){
				if(this.imgList[i][0] == '/'){
					localImg = config.BaseURL + this.imgList[i].substr(1);
				}else{
					localImg = config.BaseURL + this.imgList[i];
				}
			}else{
				localImg = this.imgList[i];
			}
			this.imgLocalList.push(localImg);
		}
		modal.show();

		if(this.isViewer2 && $(".viewer-container").length >0){
			this.viewer2.destroy();
		}
    }
    // 关闭图片查看
    closeImgModal2(modal){
		modal.hide();
		this.imgList = [];
		this.imgLocalList = [];
    }
    	//图片放大
	bigimg(type,row){
        this.showBigImg(type);
    }
    showBigImg(type){
		var that = this;
		var imgList
		if(type == 'Modal2'){
			imgList = $(".imagesInfo2")[0];
			this.viewer2 = new Viewer(imgList, {
				url: 'src',
				title: function (image) {
					return image.alt + ' (' + (this.index + 1) + '/' + this.length + ')' ; 
				},
			});
			that.isViewer2 = true
		}
		
	}
    expressCompany:any = ""; // 快递公司
    expressNumber:any = ""; // 快递单号
    expressList:any = []; // 物流信息情况
    // 订单物流的弹框展示
    showExpress(row,modal){
        let reqdata = {id:row.id}
        this.expressCompany = row.expressDeliveryUnit;
        this.expressNumber = row.expressDeliveryNumber;
        this.jsonApiService.http_obj("orderLogistics/getOrderLogisticsInfoByDeviceDamageId.html",reqdata).subscribe(res=> {
            let list = res.data.data
            this.expressList = list?list:[];
        })
        modal.show();
    }
    expressModalClose(modal){
        modal.hide();
    }
}